/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       options_dialog.cpp

	$Header: /game/options_dialog.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "options_dialog.h"

#include "adaptor_handler.h"
#include "bitmap_group_cache.h"
#include "bitmap_group_ptr.h"
#include "bitmap_layer_cache_window.h"
#include "bound_handler.h"
#include "button_cache.h"
#include "external_string.h"
#include "interface_help.h"
#include "platform.h"
#include "options.h"
#include "scrollbar.h"
#include "standard_fonts.h"
#include "text_window.h"
#include "windows_game.h"

extern t_external_string const k_text_ok( "Ok" );


static t_bitmap_group_cache    g_options_bitmaps( "dialog.options" );

namespace 
{ // unnamed namespace
    t_external_string const k_text_screen_resolution( "screen_resolution" );
    t_external_string const k_text_quick_combat( "options_quick_combat.misc" );
    t_external_string const k_text_show_coordinates( "options_show_coordinates.misc" );
    t_external_string const k_text_move_army_reminder( "options_army_reminder.misc" );
    t_external_string const k_text_movement_path( "options_movement_path.misc" );
    t_external_string const k_text_title( "options_title.misc" );
    t_external_string const k_text_right_options( "options_right_options.misc" );
	t_external_string const k_text_show_adventure_animations( "options_adventure_animations.misc" );
}

extern t_button_cache g_large_button;

// ---------------------------------------------------------------------
// options dialog.  Currently only show screen mode / resolution.
// ---------------------------------------------------------------------
t_options_dialog::t_options_dialog( t_window* parent )
                : t_dialog_options_base( parent, g_options_bitmaps.get() )
{
	t_screen_rect rect;

	t_screen_point          origin(0,0);
    

	t_text_window* text_window;
	
	m_resolution = t_window::get_main_window()->get_width();
	m_quick_combat = use_quick_combat();
    m_show_adventure_coordinates = show_adventure_coordinates();
    m_move_army_reminder = army_movement_reminder();
    m_show_move_path = show_move_path();
	m_show_adventure_animations = show_adventure_animations();

    // Title text.
	rect = m_bitmaps->find( "title" )->get_rect();
	text_window = new t_text_window( get_font( rect.height() ), rect, m_background,
		                             k_text_title, t_pixel_24(0,0,0));
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200) );
	text_window->set_center_horizontal();

	// add "screen resolution" title
	rect = m_bitmaps->find( "resolution_text" )->get_rect();
	text_window = new t_text_window( get_font( rect.height() ), rect, m_background,
		                             k_text_screen_resolution, t_pixel_24(0,0,0));
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200) );
	text_window->set_center_horizontal();

    // Right options text.
	rect = m_bitmaps->find( "options" )->get_rect();
	text_window = new t_text_window( get_font( rect.height() ), rect, m_background,
		                             k_text_right_options, t_pixel_24(0,0,0));
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200) );
	text_window->set_center_horizontal();

	create_checkboxes();
	create_scrollbars();
}

// ---------------------------------------------------------------------
// create a checkbox
// ---------------------------------------------------------------------


// ---------------------------------------------------------------------
// options dialog. 
// create checkboxes
// ---------------------------------------------------------------------
void t_options_dialog::create_checkboxes()
{
	// add "800x600" checkbox
	t_toggle_button* checkbox;
	t_screen_rect    rect;
	t_button_handler handler;
	t_help_block const&   options_help = get_help_block( "options_menu" );

	// add "quick combat" checkbox
	handler = bound_handler( *this, &t_options_dialog::quick_combat_clicked );
	checkbox = add_checkbox( "quick_combat", handler, m_quick_combat, k_text_quick_combat );
	set_help( checkbox, options_help, "quick_combat" );

    // add "coordinates" checkbox
	handler = bound_handler( *this, &t_options_dialog::coordinates_clicked );
	checkbox = add_checkbox( "coordinates", handler, m_show_adventure_coordinates,
							 k_text_show_coordinates );
	set_help( checkbox, options_help, "coordinates" );

    // add "army reminder" checkbox.	
    handler = bound_handler( *this, &t_options_dialog::move_army_reminder_clicked );
	checkbox = add_checkbox( "army_reminder", handler, m_move_army_reminder, 
							 k_text_move_army_reminder );
	set_help( checkbox, options_help, "show_army_reminder" );
    
    // add "show movement path" checkbox.	
    handler = bound_handler( *this, &t_options_dialog::show_move_path_clicked );
	checkbox = add_checkbox( "movement_path", handler, m_show_move_path, k_text_movement_path );
	set_help( checkbox, options_help, "show_movement_path" );

	// add "show animations" checkbox
	handler = bound_handler( *this, &t_options_dialog::adventure_animations_clicked );
	checkbox = add_checkbox( "animation", handler, m_show_adventure_animations, 
							 k_text_show_adventure_animations );
	set_help( checkbox, options_help, "animation" );
}

// ---------------------------------------------------------------------
// create scrollbars
// ---------------------------------------------------------------------
static t_external_string const k_text_your_armies( "Your_armies.options" );
static t_external_string const k_text_other_armies( "Other_armies.options" );
static t_external_string const k_text_off( "speed_off.options" );

void t_options_dialog::create_scrollbars()
{
	t_scrollbar*        scrollbar;
	t_screen_rect       rect;
	t_scrollbar_handler handler;
	t_help_block const&   options_help = get_help_block( "options_menu" );

	// animation speeds
	m_army_speed = get_army_animation_speed();
	m_army_speed_text_window = create_text_with_value( "army_speed_text", k_text_your_armies, m_army_speed );
	rect = m_bitmaps->find( "army_animation_speed" )->get_rect();
	scrollbar = new t_scrollbar( rect.top_left(), rect.width(), m_background, -2, 6, false );
	scrollbar->set_position( convert_speed_from_linear( m_army_speed ) );
	handler = bound_handler( *this, &t_options_dialog::army_speed_change );
	scrollbar->set_handler( handler );
	set_help( scrollbar, options_help, "army_speed" );

	m_enemy_speed = get_enemy_animation_speed();
	m_enemy_speed_text_window = create_text_with_value( "enemy_speed_text", k_text_other_armies, m_enemy_speed );
	if (m_enemy_speed == 800)
		m_enemy_speed_text_window->set_text( k_text_off );
	rect = m_bitmaps->find( "enemy_animation_speed" )->get_rect();
	scrollbar = new t_scrollbar( rect.top_left(), rect.width(), m_background, -2, 6, false );
	scrollbar->set_position( convert_speed_from_linear( m_enemy_speed ));
	handler = bound_handler( *this, &t_options_dialog::enemy_speed_change );
	scrollbar->set_handler( handler );
	set_help( scrollbar, options_help, "enemy_speed" );
}

void t_options_dialog::adventure_animations_clicked( t_button* )
{
	m_show_adventure_animations = !m_show_adventure_animations;
}

// ---------------------------------------------------------------------
// options dialog. 
// ---------------------------------------------------------------------
void t_options_dialog::quick_combat_clicked( t_button* button )
{
	m_quick_combat = button->is_pressed();
}

void t_options_dialog::coordinates_clicked( t_button* button )
{
    m_show_adventure_coordinates = button->is_pressed();
}

void t_options_dialog::move_army_reminder_clicked( t_button* button )
{
    m_move_army_reminder = button->is_pressed();
}

void t_options_dialog::show_move_path_clicked( t_button* button )
{
    m_show_move_path = button->is_pressed();
}

// ---------------------------------------------------------------------
// options dialog.  Currently only show screen mode / resolution.
// ---------------------------------------------------------------------
void t_options_dialog::resolution_clicked( t_button* button, int resolution )
{
	m_resolution = resolution;
}

// ---------------------------------------------------------------------
// options dialog
// respond to speed change
// ---------------------------------------------------------------------
void t_options_dialog::army_speed_change( t_scrollbar*, int value )
{
	m_army_speed = convert_speed_to_linear( value );
    set_text_value( m_army_speed_text_window, value );
}


void t_options_dialog::enemy_speed_change( t_scrollbar* scrollbar, int value )
{
	m_enemy_speed = convert_speed_to_linear( value );
	if (value < scrollbar->get_max_value())
		set_text_value( m_enemy_speed_text_window, value );
	else
		m_enemy_speed_text_window->set_text( k_text_off );
}
// ---------------------------------------------------------------------
// options dialog
// ---------------------------------------------------------------------
void t_options_dialog::ok_clicked( t_button* button )
{
	set_army_animation_speed( m_army_speed );
	set_enemy_animation_speed( m_enemy_speed );
	set_show_enemy_moves( m_enemy_speed < 800 );
	set_quick_combat( m_quick_combat );
	set_show_adventure_animations( m_show_adventure_animations );
  set_show_adventure_coordinates( m_show_adventure_coordinates );
  set_show_move_path( m_show_move_path );
  set_army_movement_reminder( m_move_army_reminder );
	t_dialog_options_base::ok_clicked( button );
}

